<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Account_model extends CI_Model {
	public function __construct() {
		parent::__construct();
	}

	public function get_wo_pend($id){
		$sql = "SELECT uwo.* , alv.meaning FROM ubs_work_order uwo
					INNER JOIN app_lookup_values alv ON alv.lookup_code = uwo.maintain_code AND alv.lookup_type = 'UBS_MAINTAIN_TYPE'
				WHERE uwo.account_id = ".$id." AND uwo.status = 'Pending' ";
		$query = $this->db->query($sql);
		if ($query->num_rows() != 0) {
			return $query->result();
		} else {
			$result = "No results found.";
			return $result;
		}
	}

	public function insertData($data) {
		$this->db->query("call ar_entity_ins(?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$id = $keys->id;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'id' => $id,
		);
		echo json_encode($arr);
	}

	public function get_mother_meter() {
		$sql = "SELECT * FROM ubs_mother_meter ";
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function updateData($data) {
		$this->db->query("call ar_entity_upd(?,?,?,?,?,?,?,?,?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}


	public function deleteData($data){
		$this->db->query("call ar_account_del(?,?,?,?,?,@err,@msg)",$data);
		$que = $this->db->query("SELECT @err as error , @msg as msg");
		echo json_encode($que->result());
	}

	public function insertDataAddr($data) {
		$this->db->query("call ar_entity_address_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$id = $keys->id;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'id' => $id,
		);
		echo json_encode($arr);
	}

	public function updateDataAddr($data) {
		$this->db->query("call ar_entity_address_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}

	public function insertDataAcc($data) {
		$this->db->query("call ar_account_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg,@accnum)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg, @accnum as accnum");
		$id;
		$msg;
		$err;
		$accnum;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$id = $keys->id;
			$accnum = $keys->accnum;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'id' => $id,
			'accnum' => $accnum,
		);
		echo json_encode($arr);
	}

	public function insert_work_order($data) {
		$this->db->query("call ubs_work_order_ins(?,?,?,?,?,?,@id,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id, @err as error , @msg as msg");
		echo json_encode($que->result());
	}

	public function updateDataAcc($data) {
		$this->db->query("call ar_account_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}

	public function insertDataDev($data) {
		$this->db->query("call ar_device_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$id = $keys->id;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'id' => $id,
		);
		echo json_encode($arr);
	}

	public function updateDataDev($data) {
		$this->db->query("call ar_device_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
			$id = $keys->id;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
			'id' => $id,
		);
		echo json_encode($arr);
	}

	public function insertChrglist($data) {
		$this->db->query("call ar_account_chargelist_ins(?,?,?,?,?,?,?,@id,@err,@msg)", $data);
		$que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}

	public function updateChrglist($data) {
		$this->db->query("call ar_account_chargelist_upd(?,?,?,?,?,?,?,@err,@msg)", $data);
		$que = $this->db->query("SELECT @err as err,@msg as msg");
		$id;
		$msg;
		$err;
		foreach ($que->result() as $keys) {
			$msg = $keys->msg;
			$err = $keys->err;
		}
		$arr = array(
			'msg' => $msg,
			'err' => $err,
		);
		echo json_encode($arr);
	}

	public function getSearchAcct($acct_num, $acct_name, $entity_name) {
		$novalue = 1;
		$sql;
		if ($acct_num != "" && $acct_name != "" && $entity_name != "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_name LIKE " . $this->db->escape($acct_name) . " AND acct_num LIKE '" . $acct_num . "' AND entity_name   LIKE '" . $entity_name . "' ";
		} else if ($acct_num == "" && $acct_name != "" && $entity_name != "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_name LIKE " . $this->db->escape($acct_name) . " AND entity_name LIKE '" . $entity_name . "' ";
		} else if ($acct_num != "" && $acct_name != "" && $entity_name == "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_num LIKE " . $this->db->escape($acct_num) . " AND acct_name LIKE '" . $acct_name . "' ";
		} else if ($acct_num != "" && $acct_name == "" && $entity_name != "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_num LIKE " . $this->db->escape($acct_num) . " AND acct_num LIKE '" . $acct_num . "' ";
		} else if ($acct_num != "" && $acct_name == "" && $entity_name == "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_num LIKE " . $this->db->escape($acct_num) . "";
		} else if ($acct_num == "" && $acct_name != "" && $entity_name == "") {
			$sql = "SELECT * FROM ar_account_v WHERE acct_name LIKE " . $this->db->escape($acct_name) . "";
		} else if ($acct_num == "" && $acct_name == "" && $entity_name != "") {
			$sql = "SELECT * FROM ar_account_v WHERE entity_name LIKE " . $this->db->escape($entity_name) . "";
		} else {
			$novalue = 0;
		}

		if ($novalue = 1) {
			$query = $this->db->query($sql);
			if ($query->num_rows() != 0) {
				return $query->result();
			} else {
				$result = "No results found.";
				return $result;
			}
		} else {
			$result = "No results found.";
			return $result;
			$novalue = 1;
		}
	}

	public function getchglist($id) {
		$this->db->where('account_id', $id);
		$query = $this->db->get('ar_account_chargelist_v');
		return $query->result();
	}

	public function getChrglst() {
		$query = $this->db->get('ubs_chg_list');
		return $query->result();
	}

	public function getChrglstId($id) {
		$this->db->where('account_id', $id);
		$query = $this->db->get('ar_account_chargelist_v');
		return $query->result();
	}

	public function getDevice($id) {

		$this->db->where('account_id', $id);
		$query = $this->db->get('ar_device_v');
		return $query->result();

	}

	public function countries() {
		$query = $this->db->get('ar_countries');
		return $query->result();
	}

	public function getDevice2($id) {
		$this->db->where('device_assignment_id', $id);
		$query = $this->db->get('ar_device_v');
		return $query->result();
	}

	public function getEntities($id) {
		$this->db->where('entity_id', $id);
		$query = $this->db->get('ar_entity_address_v');
		return $query->result();
	}

	public function getAddr($id, $acode) {
		$this->db->where('entity_id', $id);
		$this->db->where('addr_code', $acode);
		$query = $this->db->get('ar_entity_address');
		return $query->result();
	}

	public function get_dev_type() {
		$query = $this->db->get('ar_device_type_v');
		return $query->result();
	}

	public function get_dev_stats() {
		$query = $this->db->get('ar_device_status_v');
		return $query->result();
	}

	public function get_item() {
		$this->db->where('item_type','INV');
		$query = $this->db->get('inv_items');
		return $query->result();
	}

	public function getAddr2($id) {
		$this->db->where('entity_id', $id);
		$query = $this->db->get('ar_entity_address');
		return $query->result();
	}

	public function getEntity($id) {
		$this->db->where('entity_id', $id);
		$query = $this->db->get('ar_entity_v');
		return $query->result();
	}

	public function get_e_type() {
		$query = $this->db->get('ar_entity_type_v');
		return $query->result();
	}

	public function get_acc_type() {
		$query = $this->db->get('ar_account_types_v');
		return $query->result();
	}

	public function get_acc_stats() {
		$query = $this->db->get('ar_account_status_v');
		return $query->result();
	}

	public function get_addr_pur() {
		$query = $this->db->get('ar_address_purpose_v');
		return $query->result();
	}

	public function getAllAddr($id, $acode) {
		$this->db->where('entity_id', $id);
		$this->db->where('address_id', $acode);
		$query = $this->db->get('ar_entity_address');
		return $query->result();
	}

	public function getAllAddr2($id) {
		$this->db->where('entity_id', $id);
		$query = $this->db->get('ar_entity_address');
		return $query->result();
	}

	public function getacct2($id) {
		$this->db->where('entity_id', $id);
		$query = $this->db->get('ar_account_v');
		return $query->result();
	}

	public function getacct($id) {
		$this->db->where('account_id', $id);
		$query = $this->db->get('ar_account_v');
		return $query->result();
	}

	public function getcuracctnum($val) {
		$this->db->query("call ar_get_account_num(?,@val)", $val);
		$que = $this->db->query("SELECT @val as val");
		//var_dump($que->result());
		return $que->result();
	}

	public function getMeterSeq(){
		return $this->db->query('SELECT (MAX(meter_seq) + 10 ) meter_seq FROM ar_account')->result();
	}

	public function getAllAccount($id, $acode, $addrcode) {
		$this->db->where('entity_id', $id);
		$this->db->where('account_id', $acode);
		$this->db->where('acct_addr', $addrcode);
		$query = $this->db->get('ar_account_v');
		return $query->result();
	}
	public function getEnaAddr($id) {
		$this->db->where('entity_id', $id);
		$this->db->where('enable_flag', 'Y');
		$query = $this->db->get('ar_entity_address_v');
		return $query->result();
	}
	public function getZones($id, $acode) {
		$this->db->where('entity_id', $id);
		$this->db->where('address_id', $acode);
		$query = $this->db->get('ar_entity_address_v');
		return $query->result();
	}

	public function getcycleunit() {
		$this->db->where('lookup_type', "CYCLE_UNIT");
		$q = $this->db->get('app_lookup_values');
		return $q->result();
	}

	public function getSearch($etype, $ename) {
		$sql;
		if ($etype != "" && $ename != "") {
			$sql = "SELECT * FROM ar_entity_v WHERE entity_type LIKE " . $this->db->escape($etype) . " AND entity_name LIKE '" . $ename . "' ";
		} else if ($etype == "" && $ename != "") {
			$sql = "SELECT * FROM ar_entity_v WHERE entity_name LIKE " . $this->db->escape($ename) . " ";
		} else {
			$sql = "SELECT * FROM ar_entity_v WHERE entity_type LIKE " . $this->db->escape($etype) . " ";
		}
		//$query = $this->db->get_where('ar_modules', array('entity_type' => $etype, 'entity_name' => $ename), 1);
		$query = $this->db->query($sql);
		if ($query->num_rows() != 0) {
			return $query->result();
		} else {
			$result = "No results found.";
			return $result;
		}
	}

	public function get_lookup($lookup){
		$this->db->where('lookup_type',$lookup);
		$q = $this->db->get('app_lookup_values');
		return $q->result();
	}

	public function getzoning($zone) {
		$this->db->where('zone_code', $zone);
		$query = $this->db->get('ar_zones');
		return $query->result();
	}

	public function getzoning2($zone) {
		$this->db->where('zone_code', $zone);
		$query = $this->db->get('ar_zone_values');
		return $query->result();
	}

	public function getwoowner($a){
		$id;
		$q = $this->db->query('SELECT meaning FROM app_lookup_values WHERE lookup_code = "'.$a.'" AND enable_flag = "Y"');
		foreach($q->result() as $we){
			$id = $we->meaning;
		}
		return $id;
	}
}